import { goodsDetail } from "../../../data/goods";
const app=getApp();
Page({
  data: {
    data:{price:0,delPrice:0},
    swiperCurrentIndex: 1,
    isSpecShowed: false,
    selectedSpecStr:'',
    numBuys:1,
    specShowingType:'',
  },
  onLoad(option) {
    let data=goodsDetail(option.id);
    app.gData.goodsDetail=data;
    this.setData({data:data});
    this.updateSelectedSpecStr(data.specs);
  },
  updateSelectedSpecStr(specs) {
    if(!specs)return;
    let str='';
    specs.forEach((item,index)=>{
      if(item.current>-1)
        str+=item.list[item.current].value+(index<specs.length-1?' + ':'');
      else
        str+=' ？'+(index<specs.length-1?' + ':'');
    });
    str+='（ 数量X'+this.data.numBuys+" ）";
    this.setData({selectedSpecStr:str});
    app.gData.goodsDetail.spec=str;
  },
  toComments(e) {
    wx.navigateTo({ url: '/pages/goods/detail/comments/index', });
  },
  onSwiperChange(e) {
    this.setData({ swiperCurrentIndex: e.detail.current + 1 });
  },
  showCoupon(e) {
    this.setData({ isCouponShowed: true });
  },
  onCouponClose(e) {
    this.setData({ isCouponShowed: false });
  },
  showSpec(e) {
    //console.log('showSpec');
    this.setData({ 
      isSpecShowed: true,
      specShowingType: e.currentTarget.dataset.type 
     });
  },
  onSpecClose() {
    this.setData({ isSpecShowed: false });
  },
  onSelectSpec(e) {
    //this.setData({ selectedColor: e.detail.value });
    let specs=this.data.data.specs.map((item,index)=>{
      if(item.title==e.detail.title){
        item.current=e.detail.current
      }
      return item;
    });
    this.data.data.specs=specs;
    this.updateSelectedSpecStr(specs);
  },
  onStepNumBuys(e) {
    this.setData({numBuys:e.detail.count});
    this.updateSelectedSpecStr(this.data.data.specs);
    app.gData.goodsDetail.numBuys=e.detail.count;
  },
  onShareAppMessage() {
    const customInfo = {
      imageUrl: this.data.data.images[0],
      title: this.data.data.title,
      path: "home/index",
    };
    return customInfo;
  },
  toCart(e) {
    if(this.data.selectedSpecStr.indexOf('？')>-1){
      wx.showToast({
        title: '请把规格填写完整',
      })
      return;
    }
    wx.navigateTo({ url: '/pages/cart/index?from=detail', });
  },
  toCommit(e) {
    if(this.data.specShowingType=='only_confirm_to_cart'){
      this.toCart(e);
    }else{
      if(this.data.selectedSpecStr.indexOf('？')>-1){
        wx.showToast({
          title: '请把规格填写完整',
        });
        return;
      }
      app.gData.goodsForCommit=[app.gData.goodsDetail];
      wx.navigateTo({ url: '/pages/cart/commit/index', });
    }
  },
  toHome(e) {
    wx.navigateTo({ url: '/pages/home/index'});
  },
  toMore(e) {
    wx.navigateTo({ url: '/pages/cart/more/index'});
  },
})